package com.dbq.fast.business.ypage;

import android.annotation.SuppressLint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.business.me.viewholder.DividerViewHolder;
import com.dbq.fast.business.me.viewholder.FooterViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yuanchen on 2018/5/4.
 */

public class YPageAdapter extends RecyclerView.Adapter {

    public static final int TYPE_ITEM = 0;
    public static final int TYPE_FOOTER = 1;
    public static final int TYPE_DIVIDER = 2;

    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE = 1;
    //没有加载更多
    public static final int NO_LOAD_MORE = 2;
    //上拉加载更多状态-默认为0
    private int mLoadMoreStatus = 0;

    private List<YPageBean.Page> data = new ArrayList<>();
    private OnStatusListener listener;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_y_page, parent, false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View footerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_reward, parent, false);
            return new FooterViewHolder(footerView);
        } else {
            View dividerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_of_y_page_divider, parent, false);
            return new DividerViewHolder(dividerView);
        }
    }

    @SuppressLint("StringFormatInvalid")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof ItemViewHolder) {
            ItemViewHolder itemVH = (ItemViewHolder) holder;
            YPageBean.Page info = data.get(position);
            itemVH.tvHint.setText(info.title);
            //默认前两个是可以点击的。不管是否付费
            //如果用户有付费，显示可以点击
            holder.itemView.setOnClickListener(v -> {
                //如果该用户没有付费，则提示购买
                //否者进入展示页面
//                if (info.isValid) {
                if (listener != null) {
                    listener.vistUser(info);
                }
//                } else {
////                    //TODO 否者进入购买界面
//                    if (listener != null) {
//                        listener.unvistUser(info);
//                    }
//                }
            });

        } else if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerVH = (FooterViewHolder) holder;
            switch (mLoadMoreStatus) {
                case PULLUP_LOAD_MORE:
                    footerVH.pbLoad.setVisibility(View.GONE);
                    footerVH.footerTip.setText(App.getContext().getString(R.string.pull_up_to_load_more));
                    break;
                case LOADING_MORE:
                    footerVH.pbLoad.setVisibility(View.VISIBLE);
                    footerVH.footerTip.setText(App.getContext().getString(R.string.loading_more));
                    break;
                case NO_LOAD_MORE:
                    footerVH.pbLoad.setVisibility(View.GONE);
                    footerVH.footerTip.setText(App.getContext().getString(R.string.no_more));
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    public void setData(List<YPageBean.Page> rewardInfos) {
        this.data = rewardInfos;
        notifyDataSetChanged();
    }

    public void addFooterData(List<YPageBean.Page> data) {
        if (this.data == null || data == null) return;
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void changeMoreStatus(int status) {
        mLoadMoreStatus = status;
        notifyDataSetChanged();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_hint)
        TextView tvHint;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnStatusListener(OnStatusListener listener) {
        this.listener = listener;
    }

    public interface OnStatusListener {
        void vistUser(YPageBean.Page data);

        void unvistUser(YPageBean.Page data);
    }
}
